﻿{% extends "layout.html" %}
{% block tittle %}
<title>显示屏和按键交互菜单</title>
{% endblock %}
{% block body %}
<div id="export_content">
  <div class="output_wrapper" id="output_wrapper_id">
    <h1><span>显示屏和按键交互菜单</span></h1>
    <blockquote>
      <p>显示屏和按键交互扩展板主要是为了方便在使用设备时多种模式切换，以及给与相应回显反馈。</p>
    </blockquote>
    <h3 id="hgit"><span>git链接</span></h3>
    <p>https://github.com/pedroqin/RaspberryPi-based-multi-functional-USB-Device</p>
    <h3 id="h"><span>实现功能</span></h3>
    <ul>
      <li><span>头部显示IP和模式</span></li>
      <li><span>中间部分显示菜单内容和执行结果，模拟翻页</span></li>
      <li><span>底部显示load average 和 温度</span></li>
    </ul>
    <h3 id="h-1"><span>硬件信息</span></h3>
    <h4 id="h-2"><span>扩展板引脚功能</span></h4>
    <table>
      <thead>
        <tr>
          <th style="text-align:center;">功能引脚</th>
          <th style="text-align:center;">树莓派接口(BCM)</th>
          <th>描述</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td style="text-align:center;">KEY1</td>
          <td style="text-align:center;">P21</td>
          <td>按键1GPIO</td>
        </tr>
        <tr>
          <td style="text-align:center;">KEY2</td>
          <td style="text-align:center;">P20</td>
          <td>按键2GPIO</td>
        </tr>
        <tr>
          <td style="text-align:center;">KEY3</td>
          <td style="text-align:center;">P16</td>
          <td>按键3GPIO</td>
        </tr>
        <tr>
          <td style="text-align:center;">摇杆UP</td>
          <td style="text-align:center;">P6</td>
          <td>摇杆上</td>
        </tr>
        <tr>
          <td style="text-align:center;">摇杆Down</td>
          <td style="text-align:center;">P19</td>
          <td>摇杆下</td>
        </tr>
        <tr>
          <td style="text-align:center;">摇杆Left</td>
          <td style="text-align:center;">P5</td>
          <td>摇杆左</td>
        </tr>
        <tr>
          <td style="text-align:center;">摇杆Right</td>
          <td style="text-align:center;">P26</td>
          <td>摇杆右</td>
        </tr>
        <tr>
          <td style="text-align:center;">摇杆Press</td>
          <td style="text-align:center;">P13</td>
          <td>摇杆按下</td>
        </tr>
        <tr>
          <td style="text-align:center;">SCLK</td>
          <td style="text-align:center;">P11/SCLK</td>
          <td>SPI时钟线</td>
        </tr>
        <tr>
          <td style="text-align:center;">MOSI</td>
          <td style="text-align:center;">P10/MOSI</td>
          <td>SPI数据线</td>
        </tr>
        <tr>
          <td style="text-align:center;">CS</td>
          <td style="text-align:center;">P8/CE0</td>
          <td>片选</td>
        </tr>
        <tr>
          <td style="text-align:center;">DC</td>
          <td style="text-align:center;">P25</td>
          <td>数据/命令选择</td>
        </tr>
        <tr>
          <td style="text-align:center;">RST</td>
          <td style="text-align:center;">P27</td>
          <td>复位</td>
        </tr>
        <tr>
          <td style="text-align:center;">BL</td>
          <td style="text-align:center;">P24</td>
          <td>背光</td>
        </tr>
      </tbody>
    </table>
    <h4 id="hlcd"><span>LCD 及其控制器</span></h4>
    <blockquote>
      <p>本款LCD使用的内置控制器为ST7789VM，是一款240 x RGB x 320像素的LCD控制器,而本LCD本身的像素为<strong>240(H)RGB x
          240(V)</strong>,同时由于初始化控制可以初始化为横屏和竖屏两种，因此LCD的内部RAM并未完全使用。
        <br>该LCD支持12位，16位以及18位每像素的输入颜色格式，即RGB444，RGB565，RGB666三种颜色格式。 <br>LCD使用四线SPI通信接口，这样可以大大的节省GPIO口，同时通信是速度也会比较快
      </p>
    </blockquote>
    <h3 id="h-3"><span>实现</span></h3>
    <h4 id="h-4"><span>实现思路</span></h4>
    <p>
      由于是要做较上层应用，故不必纠结于底层和驱动屏幕，只需在其实现的基础上做开发即可。店家提供了<code>c</code>,<code>python2</code>的操作扩展板的示例程序和使用<code>fbtft</code>驱动显示屏的示例程序,这里我们在python2示例程序基础上开发。
    </p>
    <img src="{{ url_for('static',filename='5.png') }}" />
    <p>如果忽略LCD扩展版底层实现，以显示图片为一个功能模块，则符合以下流程
    <img src="{{ url_for('static',filename='8.png') }}" />
    <pre><code class="hljs php"><span class="linenum hljs-number">1</span>graph&nbsp;TB<br><span class="linenum hljs-number">2</span>A[初始化]--&gt;B[解析菜单]<br><span class="linenum hljs-number">3</span>B&nbsp;--&gt;&nbsp;C[绘制图片]<br><span class="linenum hljs-number">4</span>C&nbsp;--&gt;&nbsp;D[显示图片]<br><span class="linenum hljs-number">5</span>D&nbsp;--&gt;&nbsp;E{等待按键选择}<br><span class="linenum hljs-number">6</span>E&nbsp;--&gt;&nbsp;F[处理数据]<br><span class="linenum hljs-number">7</span>F&nbsp;--&gt;&nbsp;C<br></code></pre>
    <p>即，交互菜单可以简化为<strong>获取执行按键选项==&gt;绘制图片==&gt;显示图片</strong></p>
    <h4 id="h-5"><span>屏幕分配</span></h4>
    <img src="{{ url_for('static',filename='6.png') }}" />
    <ol>
      <li><span>红色区域显示IP以及当前模式，于程序运行时初始化，后无需刷新</span></li>
      <li><span>绿色区域显示菜单以及命令内容</span></li>
      <li><span>蓝色区域显示当前load average以及温度，随绿色区域一起刷新</span></li>
    </ol>
    <h4 id="h-6"><span>按键</span></h4>
    <ol>
      <li>Key1/方向键 中 作为<code>确认</code></li>
      <li>Key2 作为<code>返回</code></li>
      <li><span>方向键 左/上，菜单及运行结果查看时上翻</span></li>
      <li><span>方向键 右/下，菜单及运行结果查看时下翻</span></li>
      <li><span>Key3/方向键“中”保留</span></li>
    </ol>
    <h4 id="h-7"><span>程序实现</span></h4>
    <h5 id="h-8"><span>扩展板初始化</span></h5>
    <pre><code class="python language-python hljs"><span class="linenum hljs-number"> 1</span><span class="hljs-keyword">import</span>&nbsp;spidev&nbsp;<span class="hljs-keyword">as</span>&nbsp;SPI<br><span class="linenum hljs-number"> 2</span><span class="hljs-keyword">import</span>&nbsp;ST7789<br><span class="linenum hljs-number"> 3</span><span class="hljs-keyword">import</span>&nbsp;time,os,sys,logging<br><span class="linenum hljs-number"> 4</span><span class="hljs-keyword">import</span>&nbsp;RPi.GPIO&nbsp;<span class="hljs-keyword">as</span>&nbsp;GPIO<br><span class="linenum hljs-number"> 5</span><span class="hljs-keyword">from</span>&nbsp;PIL&nbsp;<span class="hljs-keyword">import</span>&nbsp;Image,ImageDraw,ImageFont<br><span class="linenum hljs-number"> 6</span><br><span class="linenum hljs-number"> 7</span><span class="hljs-comment">#&nbsp;Raspberry&nbsp;Pi&nbsp;pin&nbsp;configuration:</span><br><span class="linenum hljs-number"> 8</span>RST&nbsp;=&nbsp;<span class="hljs-number">27</span><br><span class="linenum hljs-number"> 9</span>DC&nbsp;=&nbsp;<span class="hljs-number">25</span><br><span class="linenum hljs-number">10</span>BL&nbsp;=&nbsp;<span class="hljs-number">24</span><br><span class="linenum hljs-number">11</span>BUS&nbsp;=&nbsp;<span class="hljs-number">0</span><br><span class="linenum hljs-number">12</span>DEVICE&nbsp;=&nbsp;<span class="hljs-number">0</span><br><span class="linenum hljs-number">13</span><br><span class="linenum hljs-number">14</span>KEY_UP_PIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;<span class="hljs-number">6</span><br><span class="linenum hljs-number">15</span>KEY_DOWN_PIN&nbsp;&nbsp;&nbsp;=&nbsp;<span class="hljs-number">19</span><br><span class="linenum hljs-number">16</span>KEY_LEFT_PIN&nbsp;&nbsp;&nbsp;=&nbsp;<span class="hljs-number">5</span><br><span class="linenum hljs-number">17</span>KEY_RIGHT_PIN&nbsp;&nbsp;=&nbsp;<span class="hljs-number">26</span><br><span class="linenum hljs-number">18</span>KEY_PRESS_PIN&nbsp;&nbsp;=&nbsp;<span class="hljs-number">13</span><br><span class="linenum hljs-number">19</span><br><span class="linenum hljs-number">20</span>KEY1_PIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;<span class="hljs-number">21</span><br><span class="linenum hljs-number">21</span>KEY2_PIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;<span class="hljs-number">20</span><br><span class="linenum hljs-number">22</span>KEY3_PIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;<span class="hljs-number">16</span><br><span class="linenum hljs-number">23</span><br><span class="linenum hljs-number">24</span><span class="hljs-comment">#&nbsp;240x240&nbsp;display&nbsp;with&nbsp;hardware&nbsp;SPI:</span><br><span class="linenum hljs-number">25</span>disp&nbsp;=&nbsp;ST7789.ST7789(SPI.SpiDev(BUS,&nbsp;DEVICE),RST,&nbsp;DC,&nbsp;BL)<br><span class="linenum hljs-number">26</span>disp.Init()&nbsp;<span class="hljs-comment">#&nbsp;Initialize&nbsp;library.</span><br><span class="linenum hljs-number">27</span>disp.clear()&nbsp;<span class="hljs-comment">#&nbsp;Clear&nbsp;display.</span><br><span class="linenum hljs-number">28</span><br><span class="linenum hljs-number">29</span><span class="hljs-comment">#init&nbsp;GPIO</span><br><span class="linenum hljs-number">30</span>GPIO.setmode(GPIO.BCM)<br><span class="linenum hljs-number">31</span>GPIO.setup(KEY_UP_PIN,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GPIO.IN,&nbsp;pull_up_down=GPIO.PUD_UP)&nbsp;<span class="hljs-comment">#&nbsp;Input&nbsp;with&nbsp;pull-up</span><br><span class="linenum hljs-number">32</span>GPIO.setup(KEY_DOWN_PIN,&nbsp;&nbsp;&nbsp;&nbsp;GPIO.IN,&nbsp;pull_up_down=GPIO.PUD_UP)&nbsp;<span class="hljs-comment">#&nbsp;Input&nbsp;with&nbsp;pull-up</span><br><span class="linenum hljs-number">33</span>GPIO.setup(KEY_LEFT_PIN,&nbsp;&nbsp;&nbsp;&nbsp;GPIO.IN,&nbsp;pull_up_down=GPIO.PUD_UP)&nbsp;<span class="hljs-comment">#&nbsp;Input&nbsp;with&nbsp;pull-up</span><br><span class="linenum hljs-number">34</span>GPIO.setup(KEY_RIGHT_PIN,&nbsp;&nbsp;&nbsp;GPIO.IN,&nbsp;pull_up_down=GPIO.PUD_UP)&nbsp;<span class="hljs-comment">#&nbsp;Input&nbsp;with&nbsp;pull-up</span><br><span class="linenum hljs-number">35</span>GPIO.setup(KEY_PRESS_PIN,&nbsp;&nbsp;&nbsp;GPIO.IN,&nbsp;pull_up_down=GPIO.PUD_UP)&nbsp;<span class="hljs-comment">#&nbsp;Input&nbsp;with&nbsp;pull-up</span><br><span class="linenum hljs-number">36</span>GPIO.setup(KEY1_PIN,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GPIO.IN,&nbsp;pull_up_down=GPIO.PUD_UP)&nbsp;<span class="hljs-comment">#&nbsp;Input&nbsp;with&nbsp;pull-up</span><br><span class="linenum hljs-number">37</span>GPIO.setup(KEY2_PIN,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GPIO.IN,&nbsp;pull_up_down=GPIO.PUD_UP)&nbsp;<span class="hljs-comment">#&nbsp;Input&nbsp;with&nbsp;pull-up</span><br><span class="linenum hljs-number">38</span>GPIO.setup(KEY3_PIN,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GPIO.IN,&nbsp;pull_up_down=GPIO.PUD_UP)&nbsp;<span class="hljs-comment">#&nbsp;Input&nbsp;with&nbsp;pull-up</span><br><span class="linenum hljs-number">39</span>......<br></code></pre>
    <h5 id="h-9"><span>菜单定义</span></h5>
    <pre><code class="python language-python hljs"><span class="linenum hljs-number"> 1</span>menu_str=<span class="hljs-string">"""<br><span class="linenum hljs-number"> 2</span>1.&nbsp;Get&nbsp;Status&nbsp;-&gt;&nbsp;&nbsp;./get_info.sh&nbsp;Get_Status<br><span class="linenum hljs-number"> 3</span>2.&nbsp;U&nbsp;Disk&nbsp;Mode&nbsp;-&gt;&nbsp;./get_info.sh&nbsp;U-DiskMode<br><span class="linenum hljs-number"> 4</span>3.&nbsp;Ether&nbsp;Mode&nbsp;-&gt;&nbsp;./get_info.sh&nbsp;EtherMode<br><span class="linenum hljs-number"> 5</span>4.&nbsp;AP&nbsp;Mode&nbsp;-&gt;&nbsp;./get_info.sh&nbsp;EnableAP<br><span class="linenum hljs-number"> 6</span>5.&nbsp;Keyboard&nbsp;Mode&nbsp;-&gt;&nbsp;./get_info.sh&nbsp;PI-as-keyboard<br><span class="linenum hljs-number"> 7</span>6.&nbsp;Flash&nbsp;U&nbsp;Disk&nbsp;-&gt;&nbsp;./get_info.sh&nbsp;Flash_U_Disk<br><span class="linenum hljs-number"> 8</span>7.&nbsp;Re-GetIP&nbsp;-&gt;&nbsp;./get_info.sh&nbsp;Re-GetIP<br><span class="linenum hljs-number"> 9</span>8.&nbsp;Cat&nbsp;Cmdline&nbsp;-&gt;&nbsp;./get_info.sh&nbsp;CatCmdline<br><span class="linenum hljs-number">10</span>9.&nbsp;Cat&nbsp;WlanCfg&nbsp;-&gt;&nbsp;./get_info.sh&nbsp;CatWlanCfg<br><span class="linenum hljs-number">11</span>10.&nbsp;Export&nbsp;Log&nbsp;-&gt;&nbsp;./get_info.sh&nbsp;Export_log&nbsp;%s<br><span class="linenum hljs-number">12</span>11.&nbsp;REBOOT&nbsp;-&gt;&nbsp;sync;reboot<br><span class="linenum hljs-number">13</span>12.&nbsp;POWEROFF&nbsp;-&gt;&nbsp;sync;poweroff<br><span class="linenum hljs-number">14</span>13.&nbsp;EXIT&nbsp;-&gt;&nbsp;exit<br><span class="linenum hljs-number">15</span>"""</span><br></code></pre>
    <h5 id="hlog"><span>log记录</span></h5>
    <pre><code class="python language-python hljs"><span class="linenum hljs-number"> 1</span><span class="hljs-comment">#&nbsp;logger</span><br><span class="linenum hljs-number"> 2</span>logger&nbsp;=&nbsp;logging.getLogger()<br><span class="linenum hljs-number"> 3</span>logger.setLevel(logging.DEBUG)<br><span class="linenum hljs-number"> 4</span>log_name&nbsp;=&nbsp;os.path.join(<span class="hljs-string">"/tmp"</span>,<span class="hljs-string">"main_"</span>&nbsp;+&nbsp;time.strftime(<span class="hljs-string">"%Y%m%d%H%M%S"</span>,&nbsp;time.localtime())&nbsp;+&nbsp;<span class="hljs-string">".log"</span>)<br><span class="linenum hljs-number"> 5</span>file_handler&nbsp;=&nbsp;logging.FileHandler(filename&nbsp;=&nbsp;log_name,mode=<span class="hljs-string">"w"</span>)<br><span class="linenum hljs-number"> 6</span>file_handler.setLevel(logging.DEBUG)<br><span class="linenum hljs-number"> 7</span>console_hander&nbsp;=&nbsp;logging.StreamHandler()<br><span class="linenum hljs-number"> 8</span>console_hander.setLevel(logging.DEBUG)<br><span class="linenum hljs-number"> 9</span><br><span class="linenum hljs-number">10</span>formatter&nbsp;=&nbsp;logging.Formatter(<span class="hljs-string">"%(asctime)s&nbsp;%(name)s&nbsp;%(levelname)s&nbsp;%(message)s"</span>)<br><span class="linenum hljs-number">11</span>file_handler.setFormatter(formatter)<br><span class="linenum hljs-number">12</span>console_hander.setFormatter(formatter)<br><span class="linenum hljs-number">13</span>logger.addHandler(file_handler)<br><span class="linenum hljs-number">14</span>logger.addHandler(console_hander)<br></code></pre>
    <h5 id="h-10"><span>屏幕头部</span></h5>
    <pre><code class="python language-python hljs"><span class="linenum hljs-number"> 1</span><span class="hljs-comment">#&nbsp;the&nbsp;header&nbsp;had&nbsp;use&nbsp;50</span><br><span class="linenum hljs-number"> 2</span><span class="hljs-function"><span class="hljs-keyword">def</span>&nbsp;<span class="hljs-title">draw_header</span><span class="hljs-params">(color=<span class="hljs-string">"YELLOW"</span>)</span>:</span><br><span class="linenum hljs-number"> 3</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment">#&nbsp;say&nbsp;hello</span><br><span class="linenum hljs-number"> 4</span>&nbsp;&nbsp;&nbsp;&nbsp;image&nbsp;=&nbsp;Image.open(os.path.join(whereami,<span class="hljs-string">"pic/hi.jpg"</span>))<br><span class="linenum hljs-number"> 5</span>&nbsp;&nbsp;&nbsp;&nbsp;disp.ShowImage(image,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>)<br><span class="linenum hljs-number"> 6</span><br><span class="linenum hljs-number"> 7</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment">#&nbsp;Draw&nbsp;a&nbsp;white&nbsp;filled&nbsp;box&nbsp;to&nbsp;clear&nbsp;the&nbsp;image</span><br><span class="linenum hljs-number"> 8</span>&nbsp;&nbsp;&nbsp;&nbsp;draw.text((<span class="hljs-number">10</span>,&nbsp;<span class="hljs-number">10</span>),&nbsp;<span class="hljs-string">'IP&nbsp;:'</span>,&nbsp;fill&nbsp;=&nbsp;color,&nbsp;font=my_font1)<br><span class="linenum hljs-number"> 9</span>&nbsp;&nbsp;&nbsp;&nbsp;draw.text((<span class="hljs-number">10</span>,&nbsp;<span class="hljs-number">30</span>),&nbsp;os.popen(<span class="hljs-string">"ip1=$(ifconfig&nbsp;wlan0&nbsp;|&nbsp;awk&nbsp;'/inet&nbsp;/&nbsp;{print&nbsp;$2}');ip2=$(ifconfig&nbsp;usb0&nbsp;2&gt;/dev/null|&nbsp;awk&nbsp;'/inet&nbsp;/&nbsp;{print&nbsp;$2}');echo&nbsp;${ip1:-$ip2}"</span>).read(),&nbsp;fill&nbsp;=&nbsp;color,&nbsp;font=my_font2)<br><span class="linenum hljs-number">10</span>&nbsp;&nbsp;&nbsp;&nbsp;draw.text((<span class="hljs-number">120</span>,&nbsp;<span class="hljs-number">10</span>),&nbsp;<span class="hljs-string">'MODE&nbsp;:'</span>,&nbsp;fill&nbsp;=&nbsp;color,&nbsp;font=my_font1)<br><span class="linenum hljs-number">11</span>&nbsp;&nbsp;&nbsp;&nbsp;draw.text((<span class="hljs-number">120</span>,&nbsp;<span class="hljs-number">30</span>),&nbsp;os.popen(<span class="hljs-string">"cat&nbsp;/proc/cmdline&nbsp;|grep&nbsp;-oE&nbsp;'modules-load=[0-9a-z,_]+'|cut&nbsp;-d=&nbsp;-f2"</span>).read(),&nbsp;fill&nbsp;=&nbsp;color,&nbsp;font=my_font2)<br><span class="linenum hljs-number">12</span>&nbsp;&nbsp;&nbsp;&nbsp;draw.line([(<span class="hljs-number">5</span>,<span class="hljs-number">50</span>),(<span class="hljs-number">235</span>,<span class="hljs-number">50</span>)],&nbsp;fill&nbsp;=&nbsp;<span class="hljs-string">"BLACK"</span>,width&nbsp;=&nbsp;<span class="hljs-number">5</span>)<br></code></pre>
    <h5 id="h-11"><span>屏幕底部</span></h5>
    <pre><code class="python language-python hljs"><span class="linenum hljs-number"> 1</span><span class="hljs-comment">#&nbsp;load&nbsp;average&nbsp;and&nbsp;temperature,&nbsp;three&nbsp;colors&nbsp;for&nbsp;three&nbsp;ranges</span><br><span class="linenum hljs-number"> 2</span><span class="hljs-function"><span class="hljs-keyword">def</span>&nbsp;<span class="hljs-title">draw_bottom</span><span class="hljs-params">()</span>:</span><br><span class="linenum hljs-number"> 3</span>&nbsp;&nbsp;&nbsp;&nbsp;load_info&nbsp;=&nbsp;os.popen(<span class="hljs-string">"cat&nbsp;/proc/loadavg|cut&nbsp;-d&nbsp;'&nbsp;'&nbsp;-f'-3'"</span>).read()<br><span class="linenum hljs-number"> 4</span>&nbsp;&nbsp;&nbsp;&nbsp;min_load&nbsp;=&nbsp;float(load_info.split()[<span class="hljs-number">0</span>])<br><span class="linenum hljs-number"> 5</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">if</span>&nbsp;min_load&nbsp;&gt;=&nbsp;<span class="hljs-number">1</span>:<br><span class="linenum hljs-number"> 6</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;color=<span class="hljs-string">"RED"</span><br><span class="linenum hljs-number"> 7</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">elif</span>&nbsp;min_load&nbsp;&gt;=&nbsp;<span class="hljs-number">0.75</span>&nbsp;<span class="hljs-keyword">and</span>&nbsp;min_load&nbsp;&lt;&nbsp;<span class="hljs-number">1.0</span>:<br><span class="linenum hljs-number"> 8</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;color=<span class="hljs-string">"GOLD"</span><br><span class="linenum hljs-number"> 9</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">else</span>:<br><span class="linenum hljs-number">10</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;color=<span class="hljs-string">"BLUE"</span><br><span class="linenum hljs-number">11</span>&nbsp;&nbsp;&nbsp;&nbsp;draw.text((<span class="hljs-number">10</span>,&nbsp;<span class="hljs-number">225</span>),&nbsp;<span class="hljs-string">"Load:&nbsp;"</span>&nbsp;+&nbsp;load_info,&nbsp;fill&nbsp;=&nbsp;color)<br><span class="linenum hljs-number">12</span>&nbsp;&nbsp;&nbsp;&nbsp;temp_info&nbsp;=&nbsp;os.popen(<span class="hljs-string">'echo&nbsp;"scale=3;$(cat&nbsp;/sys/class/thermal/thermal_zone0/temp)/1000"&nbsp;|&nbsp;bc'</span>).read()<br><span class="linenum hljs-number">13</span>&nbsp;&nbsp;&nbsp;&nbsp;temp2float&nbsp;=&nbsp;float(temp_info)<br><span class="linenum hljs-number">14</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">if</span>&nbsp;temp2float&nbsp;&gt;=&nbsp;<span class="hljs-number">50</span>:<br><span class="linenum hljs-number">15</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;color=<span class="hljs-string">"RED"</span><br><span class="linenum hljs-number">16</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">elif</span>&nbsp;temp2float&nbsp;&gt;=&nbsp;<span class="hljs-number">45</span>&nbsp;<span class="hljs-keyword">and</span>&nbsp;temp2float&nbsp;&lt;&nbsp;<span class="hljs-number">50</span>:<br><span class="linenum hljs-number">17</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;color=<span class="hljs-string">"GOLD"</span><br><span class="linenum hljs-number">18</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">else</span>:<br><span class="linenum hljs-number">19</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;color=<span class="hljs-string">"BLUE"</span><br><span class="linenum hljs-number">20</span>&nbsp;&nbsp;&nbsp;&nbsp;draw.text((<span class="hljs-number">150</span>,&nbsp;<span class="hljs-number">225</span>),<span class="hljs-string">"Temp:&nbsp;"</span>&nbsp;+&nbsp;temp_info,&nbsp;fill&nbsp;=&nbsp;color)<br></code></pre>
    <h5 id="h-12"><span>格式化字符</span></h5>
    <p>由于扩展板屏幕宽度有限，需考虑字符串长度超出屏幕宽度时进行换行。此处将输入字串按固定长度进行分割，返回<code>list</code></p>
    <pre><code class="python language-python hljs"><span class="linenum hljs-number"> 1</span><span class="hljs-function"><span class="hljs-keyword">def</span>&nbsp;<span class="hljs-title">format_str</span><span class="hljs-params">(input_str,skip_null=<span class="hljs-number">0</span>,line_number=<span class="hljs-number">0</span>)</span>:</span><br><span class="linenum hljs-number"> 2</span>&nbsp;&nbsp;&nbsp;&nbsp;line_max_width=<span class="hljs-number">220</span><br><span class="linenum hljs-number"> 3</span>&nbsp;&nbsp;&nbsp;&nbsp;result_str=[]<br><span class="linenum hljs-number"> 4</span>&nbsp;&nbsp;&nbsp;&nbsp;index=<span class="hljs-number">0</span><br><span class="linenum hljs-number"> 5</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">for</span>&nbsp;str_tmp&nbsp;<span class="hljs-keyword">in</span>&nbsp;input_str.split(<span class="hljs-string">"\n"</span>):<br><span class="linenum hljs-number"> 6</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">if</span>&nbsp;skip_null&nbsp;:<br><span class="linenum hljs-number"> 7</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">if</span>&nbsp;<span class="hljs-keyword">not</span>&nbsp;str_tmp&nbsp;:&nbsp;<span class="hljs-keyword">continue</span><br><span class="linenum hljs-number"> 8</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;index&nbsp;+=&nbsp;<span class="hljs-number">1</span><br><span class="linenum hljs-number"> 9</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">if</span>&nbsp;line_number:&nbsp;str_tmp&nbsp;=&nbsp;str(index)&nbsp;+&nbsp;<span class="hljs-string">"&nbsp;"</span>&nbsp;+&nbsp;str_tmp<br><span class="linenum hljs-number">10</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cur_width,cur_height=my_font3.getsize(str_tmp)<br><span class="linenum hljs-number">11</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">if</span>&nbsp;cur_width&nbsp;&lt;=&nbsp;line_max_width:<br><span class="linenum hljs-number">12</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result_str.append(str_tmp)<br><span class="linenum hljs-number">13</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">else</span>:<br><span class="linenum hljs-number">14</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new_line=<span class="hljs-string">''</span><br><span class="linenum hljs-number">15</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">for</span>&nbsp;char&nbsp;<span class="hljs-keyword">in</span>&nbsp;str_tmp:<br><span class="linenum hljs-number">16</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new_line&nbsp;+=&nbsp;char<br><span class="linenum hljs-number">17</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cur_width,cur_height=my_font3.getsize(new_line)<br><span class="linenum hljs-number">18</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">if</span>&nbsp;cur_width&nbsp;&gt;&nbsp;line_max_width:<br><span class="linenum hljs-number">19</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result_str.append(new_line)<br><span class="linenum hljs-number">20</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new_line=<span class="hljs-string">'&nbsp;&nbsp;'</span><br><span class="linenum hljs-number">21</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">else</span>:&nbsp;<span class="hljs-keyword">continue</span><br><span class="linenum hljs-number">22</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result_str.append(new_line)<br><span class="linenum hljs-number">23</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">return</span>&nbsp;result_str<br></code></pre>
    <h5 id="h-13"><span>按键事件</span></h5>
    <ul>
      <li><span>按键事件需考虑按键抖动，在监测到按键后适当delay 消抖</span></li>
      <li><span>做菜单切换时根据功能会需要disable特定功能键，故作为参数带入</span></li>
    </ul>
    <pre><code class="python language-python hljs"><span class="linenum hljs-number"> 1</span><span class="hljs-function"><span class="hljs-keyword">def</span>&nbsp;<span class="hljs-title">listen_kbd</span><span class="hljs-params">(source_list,enable_ok=<span class="hljs-number">1</span>,enable_cancel=<span class="hljs-number">1</span>)</span>:</span><br><span class="linenum hljs-number"> 2</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">global</span>&nbsp;select_index<br><span class="linenum hljs-number"> 3</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">while</span>&nbsp;<span class="hljs-number">1</span>:<br><span class="linenum hljs-number"> 4</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">if</span>&nbsp;<span class="hljs-keyword">not</span>&nbsp;GPIO.input(KEY_UP_PIN):<br><span class="linenum hljs-number"> 5</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select_index&nbsp;-=&nbsp;<span class="hljs-number">1</span><br><span class="linenum hljs-number"> 6</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;show(source_list)<br><span class="linenum hljs-number"> 7</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time.sleep(interval_second)<br><span class="linenum hljs-number"> 8</span><br><span class="linenum hljs-number"> 9</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">if</span>&nbsp;<span class="hljs-keyword">not</span>&nbsp;GPIO.input(KEY_LEFT_PIN):<br><span class="linenum hljs-number">10</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select_index&nbsp;-=&nbsp;<span class="hljs-number">1</span><br><span class="linenum hljs-number">11</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;show(source_list)<br><span class="linenum hljs-number">12</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time.sleep(interval_second)<br><span class="linenum hljs-number">13</span><br><span class="linenum hljs-number">14</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">if</span>&nbsp;<span class="hljs-keyword">not</span>&nbsp;GPIO.input(KEY_RIGHT_PIN):<br><span class="linenum hljs-number">15</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select_index&nbsp;+=&nbsp;<span class="hljs-number">1</span><br><span class="linenum hljs-number">16</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;show(source_list)<br><span class="linenum hljs-number">17</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time.sleep(interval_second)<br><span class="linenum hljs-number">18</span><br><span class="linenum hljs-number">19</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">if</span>&nbsp;<span class="hljs-keyword">not</span>&nbsp;GPIO.input(KEY_DOWN_PIN):<br><span class="linenum hljs-number">20</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select_index&nbsp;+=&nbsp;<span class="hljs-number">1</span><br><span class="linenum hljs-number">21</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;show(source_list)<br><span class="linenum hljs-number">22</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time.sleep(interval_second)<br><span class="linenum hljs-number">23</span><br><span class="linenum hljs-number">24</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">if</span>&nbsp;<span class="hljs-keyword">not</span>&nbsp;GPIO.input(KEY_PRESS_PIN):<br><span class="linenum hljs-number">25</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(<span class="hljs-string">"CENTER"</span>)<br><span class="linenum hljs-number">26</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time.sleep(interval_second)<br><span class="linenum hljs-number">27</span><br><span class="linenum hljs-number">28</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">if</span>&nbsp;<span class="hljs-keyword">not</span>&nbsp;GPIO.input(KEY1_PIN):<br><span class="linenum hljs-number">29</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">if</span>&nbsp;<span class="hljs-keyword">not</span>&nbsp;enable_ok:&nbsp;<span class="hljs-keyword">continue</span><br><span class="linenum hljs-number">30</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.debug(<span class="hljs-string">"KEY1,ok"</span>)<br><span class="linenum hljs-number">31</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;show(source_list,<span class="hljs-number">1</span>)<br><span class="linenum hljs-number">32</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time.sleep(interval_second)<br><span class="linenum hljs-number">33</span><br><span class="linenum hljs-number">34</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">if</span>&nbsp;<span class="hljs-keyword">not</span>&nbsp;GPIO.input(KEY2_PIN):<br><span class="linenum hljs-number">35</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">if</span>&nbsp;<span class="hljs-keyword">not</span>&nbsp;enable_cancel:&nbsp;<span class="hljs-keyword">continue</span><br><span class="linenum hljs-number">36</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.debug(<span class="hljs-string">"KEY2,cancel"</span>)<br><span class="linenum hljs-number">37</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time.sleep(interval_second)<br><span class="linenum hljs-number">38</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">break</span><br><span class="linenum hljs-number">39</span><br><span class="linenum hljs-number">40</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">if</span>&nbsp;<span class="hljs-keyword">not</span>&nbsp;GPIO.input(KEY3_PIN):<br><span class="linenum hljs-number">41</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.debug(<span class="hljs-string">"KEY3,reset"</span>)<br><span class="linenum hljs-number">42</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time.sleep(interval_second)<br></code></pre>
    <h5 id="h-14"><span>绘制图片</span></h5>
    <ul>
      <li><span>选中行用红色底纹标示</span></li>
      <li><span>同时要考虑到一页最多显示17行，实现翻页功能</span></li>
    </ul>
    <pre><code class="python language-python hljs"><span class="linenum hljs-number"> 1</span><span class="hljs-comment">#&nbsp;begin&nbsp;&gt;=&nbsp;0:begin&nbsp;at&nbsp;$begin&nbsp;line&nbsp;of&nbsp;centent&nbsp;,-1&nbsp;begin&nbsp;at&nbsp;end&nbsp;</span><br><span class="linenum hljs-number"> 2</span><span class="hljs-function"><span class="hljs-keyword">def</span>&nbsp;<span class="hljs-title">draw_str</span><span class="hljs-params">(centent,begin=<span class="hljs-number">0</span>,selected=<span class="hljs-number">0</span>,color=<span class="hljs-string">"BLUE"</span>)</span>:</span><br><span class="linenum hljs-number"> 3</span>&nbsp;&nbsp;&nbsp;&nbsp;x_offset&nbsp;=&nbsp;<span class="hljs-number">10</span><br><span class="linenum hljs-number"> 4</span>&nbsp;&nbsp;&nbsp;&nbsp;y_offset&nbsp;=&nbsp;<span class="hljs-number">50</span><br><span class="linenum hljs-number"> 5</span>&nbsp;&nbsp;&nbsp;&nbsp;single_line_height&nbsp;=&nbsp;<span class="hljs-number">10</span><br><span class="linenum hljs-number"> 6</span>&nbsp;&nbsp;&nbsp;&nbsp;max_width=<span class="hljs-number">220</span><br><span class="linenum hljs-number"> 7</span>&nbsp;&nbsp;&nbsp;&nbsp;index_cur=<span class="hljs-number">0</span><br><span class="linenum hljs-number"> 8</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">if</span>&nbsp;begin&nbsp;&gt;=&nbsp;<span class="hljs-number">0</span>&nbsp;:<br><span class="linenum hljs-number"> 9</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;centent=centent[begin:begin+max_lines_can_be_shown]<br><span class="linenum hljs-number">10</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">elif</span>&nbsp;begin&nbsp;&lt;&nbsp;<span class="hljs-number">0</span>&nbsp;:<br><span class="linenum hljs-number">11</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;centent=centent[-max_lines_can_be_shown:]<br><span class="linenum hljs-number">12</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">for</span>&nbsp;line&nbsp;<span class="hljs-keyword">in</span>&nbsp;centent:<br><span class="linenum hljs-number">13</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">if</span>&nbsp;index_cur&nbsp;==&nbsp;selected:<br><span class="linenum hljs-number">14</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;draw.rectangle((x_offset,y_offset+(index_cur)*single_line_height,x_offset+max_width,y_offset+(index_cur+<span class="hljs-number">1</span>)*single_line_height),fill&nbsp;=&nbsp;<span class="hljs-string">"RED"</span>,&nbsp;outline=<span class="hljs-number">0</span>)<br><span class="linenum hljs-number">15</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;draw.text((x_offset,&nbsp;y_offset+(index_cur)*single_line_height),&nbsp;line,&nbsp;fill&nbsp;=&nbsp;color,&nbsp;font=my_font3)<br><span class="linenum hljs-number">16</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;index_cur&nbsp;+=&nbsp;<span class="hljs-number">1</span><br><span class="linenum hljs-number">17</span><br><span class="linenum hljs-number">18</span><span class="hljs-comment">#&nbsp;begin&nbsp;at&nbsp;50&nbsp;,&nbsp;end&nbsp;at&nbsp;235,&nbsp;minus&nbsp;the&nbsp;lines&nbsp;used&nbsp;by&nbsp;bottom&nbsp;:10&nbsp;,so&nbsp;draw_info&nbsp;begin&nbsp;at&nbsp;(5,50)&nbsp;and&nbsp;end&nbsp;at&nbsp;(225,225),&nbsp;width&nbsp;:220&nbsp;,&nbsp;height&nbsp;175</span><br><span class="linenum hljs-number">19</span><span class="hljs-comment">#&nbsp;max&nbsp;lines&nbsp;:&nbsp;17&nbsp;</span><br><span class="linenum hljs-number">20</span><span class="hljs-function"><span class="hljs-keyword">def</span>&nbsp;<span class="hljs-title">draw_info</span><span class="hljs-params">(info_str,color=<span class="hljs-string">"BLUE"</span>)</span>:</span><br><span class="linenum hljs-number">21</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">global</span>&nbsp;all_centent,cur_display_first,select_index<br><span class="linenum hljs-number">22</span>&nbsp;&nbsp;&nbsp;&nbsp;draw.rectangle((<span class="hljs-number">5</span>,<span class="hljs-number">50</span>,<span class="hljs-number">235</span>,<span class="hljs-number">235</span>),&nbsp;fill&nbsp;=&nbsp;<span class="hljs-string">"WHITE"</span>,&nbsp;outline=<span class="hljs-number">0</span>)<br><span class="linenum hljs-number">23</span>&nbsp;&nbsp;&nbsp;&nbsp;draw_bottom()<br><span class="linenum hljs-number">24</span>&nbsp;&nbsp;&nbsp;&nbsp;all_centent.extend(info_str)<br><span class="linenum hljs-number">25</span><br><span class="linenum hljs-number">26</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">if</span>&nbsp;select_index&nbsp;+&nbsp;<span class="hljs-number">1</span>&nbsp;-&nbsp;max_lines_can_be_shown&nbsp;&gt;&nbsp;cur_display_first&nbsp;:<br><span class="linenum hljs-number">27</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cur_display_first&nbsp;=&nbsp;select_index&nbsp;+&nbsp;<span class="hljs-number">1</span>&nbsp;-&nbsp;max_lines_can_be_shown<br><span class="linenum hljs-number">28</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">elif</span>&nbsp;select_index&nbsp;-&nbsp;cur_display_first&nbsp;&lt;=&nbsp;<span class="hljs-number">0</span>&nbsp;:<br><span class="linenum hljs-number">29</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cur_display_first&nbsp;=&nbsp;select_index<br><span class="linenum hljs-number">30</span>&nbsp;&nbsp;&nbsp;&nbsp;draw_str(all_centent&nbsp;,&nbsp;begin&nbsp;=&nbsp;cur_display_first&nbsp;,&nbsp;selected=select_index&nbsp;-&nbsp;cur_display_first)<br><span class="linenum hljs-number">31</span>&nbsp;&nbsp;&nbsp;&nbsp;disp.ShowImage(image1,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>)<br></code></pre>
    <h5 id="h-15"><span>执行菜单操作</span></h5>
    <ul>
      <li>对特殊操作执行特定动作，如退出时显示<code>bye.jpg</code>图片等</li>
      <li><span>当选中并执行操作后log显示</span></li>
      <li><span>记录操作log</span></li>
    </ul>
    <pre><code class="python language-python hljs"><span class="linenum hljs-number"> 1</span><span class="hljs-comment">#&nbsp;show&nbsp;list&nbsp;/&nbsp;show&nbsp;cmd&nbsp;is&nbsp;different</span><br><span class="linenum hljs-number"> 2</span><span class="hljs-function"><span class="hljs-keyword">def</span>&nbsp;<span class="hljs-title">show</span><span class="hljs-params">(source_list,run_cmd=<span class="hljs-number">0</span>)</span>:</span><br><span class="linenum hljs-number"> 3</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">global</span>&nbsp;select_index,cur_display_first,all_centent<br><span class="linenum hljs-number"> 4</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment">#&nbsp;clear&nbsp;all_centent</span><br><span class="linenum hljs-number"> 5</span>&nbsp;&nbsp;&nbsp;&nbsp;all_centent=[]<br><span class="linenum hljs-number"> 6</span><br><span class="linenum hljs-number"> 7</span>&nbsp;&nbsp;&nbsp;&nbsp;options_num=len(source_list)<br><span class="linenum hljs-number"> 8</span>&nbsp;&nbsp;&nbsp;&nbsp;select_index&nbsp;=&nbsp;select_index&nbsp;%&nbsp;options_num<br><span class="linenum hljs-number"> 9</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">if</span>&nbsp;<span class="hljs-keyword">not</span>&nbsp;run_cmd:<br><span class="linenum hljs-number">10</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;draw_info(source_list)<br><span class="linenum hljs-number">11</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">else</span>:<br><span class="linenum hljs-number">12</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.debug(<span class="hljs-string">"select:&nbsp;"</span>&nbsp;+&nbsp;str(select_index)&nbsp;+&nbsp;<span class="hljs-string">"&nbsp;tittle:&nbsp;"</span>&nbsp;+&nbsp;menu[select_index]&nbsp;+&nbsp;<span class="hljs-string">"&nbsp;cmd:&nbsp;"</span>&nbsp;+&nbsp;cmd[select_index])<br><span class="linenum hljs-number">13</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command_str=cmd[select_index]<br><span class="linenum hljs-number">14</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">if</span>&nbsp;command_str.find(<span class="hljs-string">"Export_log"</span>)&nbsp;&gt;&nbsp;<span class="hljs-number">0</span>:<br><span class="linenum hljs-number">15</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command_str=command_str&nbsp;%&nbsp;log_name<br><span class="linenum hljs-number">16</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">if</span>&nbsp;command_str.strip()&nbsp;==&nbsp;<span class="hljs-string">"exit"</span>&nbsp;:<br><span class="linenum hljs-number">17</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;image&nbsp;=&nbsp;Image.open(os.path.join(whereami,<span class="hljs-string">"pic/bye.jpg"</span>))<br><span class="linenum hljs-number">18</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;disp.ShowImage(image,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>)<br><span class="linenum hljs-number">19</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sys.exit(<span class="hljs-number">0</span>)<br><span class="linenum hljs-number">20</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">elif</span>&nbsp;command_str.find(<span class="hljs-string">"poweroff"</span>)&nbsp;&gt;&nbsp;<span class="hljs-number">0</span>:<br><span class="linenum hljs-number">21</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;image&nbsp;=&nbsp;Image.open(os.path.join(whereami,<span class="hljs-string">"pic/bye.jpg"</span>))<br><span class="linenum hljs-number">22</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;disp.ShowImage(image,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>)<br><span class="linenum hljs-number">23</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd_result=os.popen(command_str).read()<br><span class="linenum hljs-number">24</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword">else</span>:<br><span class="linenum hljs-number">25</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select_index_bp=select_index<br><span class="linenum hljs-number">26</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cur_display_first_bp=cur_display_first<br><span class="linenum hljs-number">27</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select_index=<span class="hljs-number">0</span><br><span class="linenum hljs-number">28</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cur_display_first=<span class="hljs-number">1</span><br><span class="linenum hljs-number">29</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;draw_info(format_str(<span class="hljs-string">"Command:&nbsp;\n"</span>&nbsp;+&nbsp;command_str))<br><span class="linenum hljs-number">30</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;draw_info(format_str(<span class="hljs-string">"Waiting..."</span>))<br><span class="linenum hljs-number">31</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd_result=os.popen(command_str).read()<br><span class="linenum hljs-number">32</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.debug(<span class="hljs-string">"Command:\n"</span>&nbsp;+&nbsp;command_str&nbsp;+&nbsp;<span class="hljs-string">"\n"</span>&nbsp;+&nbsp;cmd_result)<br><span class="linenum hljs-number">33</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;draw_info(format_str(<span class="hljs-string">"Result:&nbsp;\n"</span>&nbsp;+&nbsp;cmd_result))<br><span class="linenum hljs-number">34</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;draw_info(format_str(<span class="hljs-string">"complete"</span>))<br><span class="linenum hljs-number">35</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;listen_kbd(all_centent,enable_ok=<span class="hljs-number">0</span>)<br><span class="linenum hljs-number">36</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select_index=select_index_bp<br><span class="linenum hljs-number">37</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cur_display_first=cur_display_first_bp<br><span class="linenum hljs-number">38</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;show(menu)<br></code></pre>
    <h3 id="h-16"><span>实现效果</span></h3>
    <video width="640" height="368" controls="controls">
      <source src="{{ url_for('static',filename='1.mp4') }}" type="video/mp4">
    </video>
    <h3 id="h-17"><span>待完善</span></h3>
    <ul>
      <li><span>由于早期直接基于商家示例做应用扩展，没有对程序做模块化编程，不利于扩展维护，待后续优化</span></li>
      <li>程序中使用的是while True轮询方式等待取键盘输入，会过多占用系统资源，后续做优化时可用<code>GPIO.add_event_detect</code>函数替代</li>
    </ul>
    <h3 id="h-18"><span>参考链接</span></h3>
    <p>店家显示器模块参考(http://www.waveshare.net/wiki/1.3inch_LCD_HAT)</p>
  </div>
</div>
{% endblock %}
